1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 import java.io.FileOutputStream;
44 import java.io.OutputStreamWriter;
45 import java.nio.charset.Charset;
46 import java.util.Locale;
47
48 public class UnicodeTest {
49
50 public static void main(String[] args) throws Exception {
51
52 String commandLineClassNameSuffix = commandLineClassNameSuffix();
53 String commandLineClassName = "ClassA" + commandLineClassNameSuffix;
54 String manifestClassName;
55 if (hasUnicodeFileSystem()) {
56 manifestClassName = "ClassB" + unicode;
57 } else {
58 manifestClassName = "ClassB" + commandLineClassNameSuffix;
59 }
60
61 generateSource(commandLineClassName, manifestClassName);
62 generateSource(manifestClassName, commandLineClassName);
63 generateManifest(manifestClassName);
64
65 System.out.println(commandLineClassName);
66 }
67
68 private static final String fileSeparator = System.getProperty("file.separator");
69 private static final String osName = System.getProperty("os.name");
70 private static final String defaultEncoding = Charset.defaultCharset().name();
71
72
73 private static final String arabic = "\u0627\u0644\u0639\u0631\u0628\u064a\u0629";
74 private static final String s_chinese = "\u4e2d\u6587";
75 private static final String t_chinese = "\u4e2d\u6587";
76 private static final String russian = "\u0440\u0443\u0441\u0441\u043A\u0438\u0439";
77 private static final String hindi = "\u0939\u093f\u0902\u0926\u0940";
78 private static final String greek = "\u03b5\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac";
79 private static final String hebrew = "\u05e2\u05d1\u05e8\u05d9\u05ea";
80 private static final String japanese = "\u65e5\u672c\u8a9e";
81 private static final String korean = "\ud55c\uad6d\uc5b4";
82 private static final String lithuanian = "Lietuvi\u0173";
83 private static final String czech = "\u010de\u0161tina";
84 private static final String turkish = "T\u00fcrk\u00e7e";
85 private static final String spanish = "espa\u00f1ol";
86 private static final String thai = "\u0e44\u0e17\u0e22";
87 private static final String unicode = arabic + s_chinese + t_chinese
88 + russian + hindi + greek + hebrew + japanese + korean
89 + lithuanian + czech + turkish + spanish + thai;
90
91 private static String commandLineClassNameSuffix() {
92
93
94
95
96
97 String[][] names = {
98 { "UTF-8", unicode, "" },
99 { "windows-1256", null, "" },
100 { "iso-8859-6", arabic, null },
101 { "GBK", s_chinese, s_chinese },
102 { "GB18030", s_chinese, s_chinese },
103 { "GB2312", s_chinese, null },
104 { "x-windows-950", null, t_chinese },
105 { "x-MS950-HKSCS", null, t_chinese },
106 { "x-euc-tw", t_chinese, null },
107 { "Big5", t_chinese, null },
108 { "Big5-HKSCS", t_chinese, null },
109 { "windows-1251", null, "" },
110 { "iso-8859-5", russian, null },
111 { "koi8-r", russian, null },
112 { "windows-1253", null, "" },
113 { "iso-8859-7", greek, null },
114 { "windows-1255", null, "" },
115 { "iso8859-8", hebrew, null },
116 { "windows-31j", null, japanese },
117 { "x-eucJP-Open", japanese, null },
118 { "x-EUC-JP-LINUX", japanese, null },
119 { "x-pck", japanese, null },
120 { "x-windows-949", null, korean },
121 { "euc-kr", korean, null },
122 { "windows-1257", null, "" },
123 { "iso-8859-13", lithuanian, null },
124 { "windows-1250", null, "" },
125 { "iso-8859-2", czech, null },
126 { "windows-1254", null, "" },
127 { "iso-8859-9", turkish, null },
128 { "windows-1252", null, "" },
129 { "iso-8859-1", spanish, null },
130 { "iso-8859-15", spanish, null },
131 { "x-windows-874", null, thai },
132 { "tis-620", thai, null },
133 };
134
135 int column;
136 if (osName.startsWith("Windows")) {
137 column = 2;
138 } else {
139 column = 1;
140 }
141 for (int i = 0; i < names.length; i++) {
142 if (names[i][0].equalsIgnoreCase(defaultEncoding)) {
143 return names[i][column];
144 }
145 }
146 return "";
147 }
148
149 private static boolean hasUnicodeFileSystem() {
150 if (osName.startsWith("Windows")) {
151 return ! osName.startsWith("Windows 9") &&
152 ! osName.equals("Windows Me");
153 } else {
154 return defaultEncoding.equalsIgnoreCase("UTF-8");
155 }
156 }
157
158 private static void generateSource(String thisClass, String otherClass) throws Exception {
159 String fileName = "UnicodeTest-src" + fileSeparator + thisClass + ".java";
160 OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8");
161 out.write("public class " + thisClass + " {\n");
162 out.write(" public static void main(String[] args) {\n");
163 out.write(" if (!" + otherClass + "." + otherClass.toLowerCase() + "().equals(\"" + otherClass + "\")) {\n");
164 out.write(" throw new RuntimeException();\n");
165 out.write(" }\n");
166 out.write(" }\n");
167 out.write(" public static String " + thisClass.toLowerCase() + "() {\n");
168 out.write(" return \"" + thisClass + "\";\n");
169 out.write(" }\n");
170 out.write("}\n");
171 out.close();
172 }
173
174 private static void generateManifest(String mainClass) throws Exception {
175 String fileName = "UnicodeTest-src" + fileSeparator + "MANIFEST.MF";
176 FileOutputStream out = new FileOutputStream(fileName);
177 out.write("Manifest-Version: 1.0\n".getBytes("UTF-8"));
178
179
180
181 byte[] headerBytes = ("Main-Class: " + mainClass + "\n").getBytes("UTF-8");
182 if (headerBytes.length <= 72) {
183 out.write(headerBytes);
184 } else {
185 out.write(headerBytes, 0, 72);
186 int start = 72;
187 while (headerBytes.length > start) {
188 out.write((byte) '\n');
189 out.write((byte) ' ');
190 int count = Math.min(71, headerBytes.length - start);
191 out.write(headerBytes, start, count);
192 start += count;
193 }
194 }
195 out.close();
196 }
197 }